home *** CD-ROM | disk | FTP | other *** search
- Path: cscsun3.larc.nasa.gov!hook
- From: hook@cscsun3.larc.nasa.gov (Ed Hook)
- Newsgroups: comp.lang.c
- Subject: Re: Float calculations
- Date: 1 Feb 1996 22:02:56 GMT
- Organization: CSC/NASA Langley Research Center
- Distribution: world
- Message-ID: <4erdag$qq5@reznor.larc.nasa.gov>
- References: <4eqssf$d9q@camelot.ccs.neu.edu> <DM458u.F1y@microunity.com>
- Reply-To: hook@cscsun3.larc.nasa.gov
- NNTP-Posting-Host: cscsun3.larc.nasa.gov
-
- In article <DM458u.F1y@microunity.com>, toms@MicroUnity.com (Tom Sanders) writes:
- |> In article <4eqssf$d9q@camelot.ccs.neu.edu>, jason@ccs.neu.edu (Jason Leatherman) writes:
- |> |>
- |> |> Hi,
- |> |>
- |> |> Check out the results I get when running this simple program on a
- |> |> Sparc/UNIX system, compiled with gcc:
- |> |>
- |> |> #include <stdio.h>
- |> |>
- |> |> void main()
- |> |> {
- |> |> float a, b;
- |> |>
- |> |> printf("%0.10f %0.10f %0.10f\n", 99974.0, 50.0, 99974.0/50.0);
- |> |>
- |> |> a = 99974.0;
- |> |> b = 50.0;
- |> |> printf("%0.10f %0.10f %0.10f\n", a, b, a/b);
- |> |> }
- |> |>
- |> |> The output is:
- |> |> 99974.0000000000 50.0000000000 1999.4800000000
- |> |> 99974.0000000000 50.0000000000 1999.4799804688
- |> |>
- |> |> Why do the divisions produce different results? This is probably some
- |> |> simple thing that I've forgotten, but I haven't figured it out yet. Does
- |> |> anyone know? Note that compiling with the -ffloat-store flag didn't make
- |> |> a difference.
- |> |> Thanks for any help,
- |> |>
- |> |> Jason
- |>
- |> For what it's worth, cc on both HP and Sun workstations do not behave
- |> this way, but gcc on both machines do.
- |>
- |> Tom Sanders
-
- Surely the explanation is as simple as noting that unadorned floating-point
- constants are 'double's -- therefore, the computations to determine the
- arguments for the first 'printf()' call are done in double precision. OTOH,
- the calculation of 'a/b' in the second call *can* be done in single
- precision and promoted to 'double'. The second poster's observation is
- then evidence that the 'cc' implementations 'double' everything, while
- 'gcc' takes a more literal approach ...
-
- --
- Ed Hook | Coppula eam, se non posit
- Computer Sciences Corporation | acceptera jocularum.
- NASA Langley Research Center | Me? Speak for my employer?...<*snort*>
- Internet: hook@cscsun3.larc.nasa.gov | ... Get a _clue_ !!! ...
-